import store from '@/store'
import { Message } from 'element-ui'
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: () => import('@/views/layout'),
    redirect: 'dashboard',
    children: [
      { path: 'dashboard', component: () => import('@/views/dashboard') },
      { path: 'article', component: () => import('@/views/article') }
    ]
  },
  { path: '/login', component: () => import('@/views/login') }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // console.log(store)
  const token = store.state.user.token
  if (!token && to.path !== '/login') {
    Message.warning('请先登录乖乖')
    return next('/login')
  }
  next()
})

export default router
